﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Daybreak.Core
{
    public class ResetableQueue<T>
        where T : class, IResetable, ICloneable<T>, new()
    {
        public static readonly T DefaultItem = new T();

        private List<T> _items;
        private int _itemsCount;

        public ResetableQueue()
        {
            _items = new List<T>();
        }

        public ResetableQueue(int count)
        {
            _items = new List<T>(count);
        }

        public void Sort(Comparison<T> comparer)
        {
            _items.Sort(comparer);
        }

        public void Reset(bool dispose)
        {
            if (dispose)
                _items.Clear();

            _itemsCount = 0;
        }

        public T Allocate()
        {


            if (_itemsCount >= _items.Count) // allocate new instance
            {
                T newItem = ResetableQueue<T>.DefaultItem.Clone();
                _items.Add(newItem);
                _itemsCount = _items.Count;

                return newItem;
            }

            // use existing instance

            int index = _itemsCount++;

            T item = _items[index];

            item.Reset();

            return item;

        }

        public T GetAt(int index)
        {
            if (index >= _itemsCount)
                return null;

            return _items[index];
        }

        public int ItemsCount
        {
            get { return _itemsCount; }
        }

    }

    public class ResetableList<T>
    {
        private List<T> _items;
        private int _itemsCount;

        public ResetableList()
        {
            _items = new List<T>();
        }

        public ResetableList(int count)
        {
            _items = new List<T>(count);
        }

        public void Sort(Comparison<T> comparer)
        {
            _items.Sort(comparer);
        }

        public void Add(T item)
        {
            if (_itemsCount >= _items.Count)
            {
                _items.Add(item);
                _itemsCount = _items.Count;
            }
            else
            {
                _items[_itemsCount++] = item;
            }
        }

        public T GetAt(int index)
        {
            return _items[index];
        }

        public void SetAt(int index, T item)
        {
            _items[index] = item;
        }

        public int IndexOf(T item)
        {
            int idx = _items.IndexOf(item);

            if (idx < _itemsCount)
                return idx;

            return -1;
        }

        public int ItemsCount
        {
            get { return _itemsCount; }
        }

        public void Reset(bool dispose)
        {
            if (dispose)
                _items.Clear();

            _itemsCount = 0;
        }
    }

    public interface IResetable
    {
        void Reset();
    }

    public interface ICloneable<T>
    {
        T Clone();
    }
}
